package com.filter;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author :wn
 * @program : 学生信息系统
 * @descript : 过滤器
 * @create :2020-11-09 22:12*/


public class SafeFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request=(HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        String url=request.getRequestURI();
        if(url.contains("/index.html")||url.contains("/login.jsp")||url.contains("/logon.jsp")||url.contains("/login.action")||url.contains("/logon.action")){
            filterChain.doFilter(request,response);
            return;
        }
      if(url.contains("/css")||url.contains("/img")||url.contains("/js")||url.contains("/layui")){
          filterChain.doFilter(request,response);
          return;
      }

        HttpSession session = request.getSession();
        if(session==null || session.getAttribute("filter")==null){
            request.setAttribute("information","请先登录!!!");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }else{
            filterChain.doFilter(request,response);
        }


    }
    @Override
    public void destroy() {

    }
}
